package com.zjj.learn.algorithmPlus.segment1.deque;


import java.util.Arrays;
import java.util.Iterator;

/**
 * 使用双向循环链表实现双端队列
 * 使用双向链表可以方便的对队头和队尾进行操作
 *
 * @author zjj_admin
 */
public class LinkedListDeque<E> implements Deque<E>, Iterable<E> {

    /**
     * 双向链表数据结构
     *
     * @param <E>
     */
    private static class Node<E> {

        /**
         * 上一个节点
         */
        Node<E> prev;

        /**
         * 当前节点数据
         */
        E value;
        /**
         * 下一个节点
         */
        Node<E> next;

        public Node(Node<E> prev, E value, Node<E> next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 循环链表哨兵节点，既表示头，也表示尾
     */
    private Node<E> sentinel;
    /**
     * 元素个数
     */
    private int size;
    /**
     * 队列的容量
     */
    private int capacity;

    public LinkedListDeque(int capacity) {
        if (capacity < 0) {
            throw new IndexOutOfBoundsException("capacity 不能小于 0");
        }
        this.capacity = capacity;
        size = 0;
        // 初始化时，哨兵的 next 为哨兵，prev 也为哨兵
        sentinel = new Node<>(null, null, null);
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
    }

    /**
     * 向队列的头部添加一个数据
     * a added b，a为哨兵
     *
     * @param value
     * @return
     */
    @Override
    public boolean offerFirst(E value) {
        if (isFull()) {
            return false;
        }
        //创建一个新节点，新节点的 prev 为哨兵，next 为哨兵的 next。
        Node<E> a = sentinel;
        Node<E> b = sentinel.next;
        Node<E> added = new Node<>(a, value, b);
        a.next = added;
        b.prev = added;
        size++;
        return true;
    }

    /**
     * a added b，b 为哨兵
     *
     * @param value
     * @return
     */
    @Override
    public boolean offerLast(E value) {
        if (isFull()) {
            return false;
        }
        //创建新节点，新节点的上一个节点为 哨兵的 prev，新节点的下一个节点为哨兵
        Node<E> a = sentinel.prev;
        Node<E> b = sentinel;
        Node<E> added = new Node<>(a, value, b);
        a.next = added;
        b.prev = added;
        size++;
        return true;
    }

    /**
     * a first b，a 为哨兵
     *
     * @return
     */
    @Override
    public E pollFirst() {
        //当队列为空时，返回 null
        if (isEmpty()) {
            return null;
        }
        Node<E> a = sentinel;
        Node<E> first = a.next;
        Node<E> b = first.next;
        a.next = b;
        b.prev = a;
        size--;
        return first.value;
    }

    /**
     * a last b，b为哨兵
     *
     * @return
     */
    @Override
    public E pollLast() {
        if (isEmpty()) {
            return null;
        }
        Node<E> b = sentinel;
        Node<E> last = b.prev;
        Node<E> a = last.prev;
        a.next = b;
        b.prev = a;
        size--;
        return last.value;
    }

    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.next.value;
    }

    @Override
    public E peekLast() {
        if (isEmpty()) {
            return null;
        }
        return sentinel.prev.value;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capacity;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p = sentinel.next;

            @Override
            public boolean hasNext() {
                return p != sentinel;
            }

            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    @SuppressWarnings("all")
    @Override
    public String toString() {
        Node<E> p = sentinel.next;
        E[] a = (E[]) new Object[size];
        int i = 0;
        while (p != sentinel) {
            a[i] = p.value;
            i++;
            p = p.next;
        }
        return Arrays.toString(a);
    }
}
